package fr.jade.service.fm.deployment;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalContentException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import fr.jade.base.JadeBaseObject;
import fr.jade.util.FractalUtil;

public class AddSubComponentTask extends JadeBaseObject implements DeploymentTask {

  protected NewComponentTask fatherTask;
  protected NewComponentTask childTask;
  
  public AddSubComponentTask(NewComponentTask fatherTask, NewComponentTask childTask){
    this.fatherTask=fatherTask;
    this.childTask=childTask;
  }
  
  public void execute() throws DeploymentException {
    log("execute :" +toString());
    Component father = fatherTask.getComponent();
    Component child = childTask.getComponent();
    
    try {
      FractalUtil.getOptContentController(father).addFcSubComponent(child);
    } catch (IllegalContentException e) {
      throw new DeploymentException("can't add child in father", e);
    } catch (IllegalLifeCycleException e) {
      throw new DeploymentException("can't add child in father", e);
    } catch (NoSuchInterfaceException e) {
      throw new DeploymentException("can't add child in father", e);
    }
  }
  
  public String getLevelName(){
    return "addComponent";
  }
  
  public String toString(){
    return "AddSubComponentTask[fatherTask:"+fatherTask+" - childTask:"+childTask+"]";
  }

}
